﻿package domino
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Vector3D;
	import flash.media.Sound;
	import flash.net.SharedObject;
	import flash.ui.Keyboard;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Point;
	import flash.text.*;
	import flash.utils.Timer;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import mochi.as3.*;

	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	
	import org.as3lib.kitchensync.*;
    import org.as3lib.kitchensync.action.*;
	import org.as3lib.kitchensync.easing.*
	import silin.utils.Hint;

	
	
	/**
	 * ...
	 * @author sergiy
	 */
	
	public  dynamic class Main extends  MovieClip
	{
	    private var hintArray:Array = [];
  		private var sequence:KSSequenceGroup;
		//private var sequence:KSParallelGroup;
		private var bmpData:BitmapData;
		private var bmp:Bitmap;
		public var hintAdded:Boolean = false;
		public var m_world:b2World;
		public var iconArray:Array = new Array(); 
		private var mousePVec:b2Vec2 = new b2Vec2();
		private var menu:mainMenu;
		private var walls:actorWall;
		private var dominArray:Array;
		private var run:Boolean = false;
		public var m_iterations:int = 10;
		public var m_timeStep:Number = 1 / 30;
		private var dominToDelete:Array = [];
		public var aboveMouse:b2Body;
		public var pixels_in_a_meter:int = 30;
		private var arrowSprite:Sprite;
		private var actDomin:actor;
		private var typeFigure:String;
		public var angleArray:Array = [];
		//private var tarclickNumber:int;
		private var coin:int = 0;
		private var selectedFigure:actor;
		private var selected:Boolean = false;
		private var domin:actor;
		private var data:Array = [];
		private var isSensor:int;
		private var dominToMove:actor;
		private var normals:Array = [];
		private var levels_scores:Array = new Array();
		private var levelsAccess:Array = new Array();
		private var numberSelectable:int = -1;
		private var LevelEnd:Boolean = false;
	    private var common_scores:Number = 0;
		//private var sound1:clickOnDomin = new clickOnDomin();
		private var timerFps:Timer = new Timer(1000, 9999);
		private var fps:int = 0;
		private var lineDrawing:Sprite = new Sprite();
		private var circleDrawing:Sprite = new Sprite();
		private var circleDrawing2:Sprite = new Sprite();
		
		private var lineDrawing2:Sprite = new Sprite();
		private var circleDrawing3:Sprite = new Sprite();
		private var circleDrawing4:Sprite = new Sprite();
		private var rotation3:Number = 0;
		private var coinArray:Array = [];
		private	var k:int = -1;
		private var intersect:b2Vec2 ;
		private var coinToDelete:Array = new Array();
		private var bg_bm:BG2;
		private var mouseDown:Boolean = false;
		private var pointXY:Point = new Point(0, 0);
		private var count:int = 0;
		private var myDataObj:SharedObject;
	
		private var _textTF:TextField = new TextField();//hint
		//private var center:b2Vec2; 
		 [Embed(source = "img/bg.svg")]
		  public var BG:Class;
		  
		   [Embed(source="img/star.svg")]
           public var starSVG:Class;
		   
		   [Embed(source="img/star2.svg")]
           public var starSVG2:Class;
		   
		
		   
		   
	   public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}//end 		
		
		private function init(e:Event = null):void 
		{
		    MochiServices.connect("e8f1fb7c28f77337",root);
			KitchenSync.initialize(this);
			Hint.register(this);
			Hint.dragHint = true;
		    myDataObj = SharedObject.getLocal("catchTheStar");
			//myDataObj.clear();
            trace(myDataObj + " myDataObj");
			//mochi.MochiServices.connect("e8f1fb7c28f77337");
			
			//addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			
			
			//var message:KSTrace = new KSTrace("HELLO, WORLD ... Sorry I'm late!", 5000);
            //message.start();
		  var bg:Sprite = new BG();
			bg.x = 0;
			bg.y = 0;
			bg.visible = true;
			
			
			bmpData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xFFFFCC);
			bmp = new Bitmap(bmpData);
			timerFps.start();
			
			addChild(bmp);
			
			addChild(bg);
			bg_bm = new BG2(this);
			
			
			removeEventListener(Event.ADDED_TO_STAGE, init);
		    createWorld();
			addArrow() ;
			initArrayForScores(30);	
			menu = new mainMenu(this, m_world, levelsAccess, levels_scores);
			menu.addScores(common_scores);
			menu.total2.text = "TOTAL SCORES " + String(common_scores);
		
			//addIcons();
			//trace(run);
			//reloadScores(null);
		//	addChild(_textTF);
			//menu.setArrayAccess(levelsAccess);
			/* var tween:KSTween = new KSTween(menu.start_button, "x", 0, 300, "3sec", "0sec", null);
                 tween.start();
			 var tween2:KSTween = tween.cloneWithTarget(menu.start_button, "y");
                 tween2.start();*/
			//walls = new actorWall(m_world, this, new Point(0, 0), actorWall.addCoord());
		
			timerFps.addEventListener(TimerEvent.TIMER_COMPLETE, fpsHandler);
			timerFps.addEventListener(TimerEvent.TIMER, timeFps);
			addEventListener(MainField.MOUSE_IN_SELECT, selectHandler);
			addEventListener(MainField.MOUSE_IN_FIELD, fieldHandler);
			addEventListener(MainField.MOUSE_IN_NULL, NullHandler);
			addEventListener(MainField.MOUSE_IN_FIGURES, menuHandler);
			addEventListener(Event.ENTER_FRAME, Update);
			addEventListener(eventRun.RUN_GAME, runGame);
			addEventListener(eventLoadLevel.LOAD_LEVEL, loadDominos);
			addEventListener(eventRun.STOP_GAME, stopHandler);
			addEventListener(eventRun.GAME_OVER, overHandler);
			
			addEventListener(eventLoadLevel.DELETE_DOMIN, deleteDomino);
			addEventListener(eventLoadLevel.DELETE_COIN, deleteCoin);
		    addEventListener(eventLoadLevel.DELETE_COIN_BLACK, deleteCoin);
		    
			addEventListener(eventLoadLevel.CLEAR_LEVEL, clearHandler);
			addEventListener(menuEvents.LEVEL_VIEW, reloadScores);
		    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
			
			//stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
			//stage.addEventListener(KeyboardEvent.KEY_DOWN, TextFieldListener2);
			
			//stage.addEventListener(MouseEvent.CLICK, clickListener);
			stage.addEventListener(MouseEvent.MOUSE_UP, upListener);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, clickListener);
			
			
		 //stage.addEventListener(MouseEvent., upListener);
			
		
			
		    //dominArray.push(walls);
		}//end
		
		private function onConnectError():void
		{
			trace("test");
		}//end
		private function updateShareData():void {
		//myDataObj.data.arrayAccess =
		 myDataObj.data.arrayScore = levels_scores;
		 myDataObj.data.score = common_scores;
		 myDataObj.data.arrayAccess = levelsAccess;
		 
		
			}//end
		private function NullHandler(e:MainField):void 
		{
	
			if (selectedFigure) {
				trace(selectedFigure + " selectedFigure!!!");
				trace(pointXY.x / 30, pointXY.y / 30);
				//selectedFigure._body.SetXForm(new b2Vec2(pointXY.x/30, pointXY.y/30), int(data[6]) * Math.PI / 180 );
				selectedFigure._body.SetXForm(new b2Vec2(pointXY.x/30, pointXY.y/30), 0);
				
				selectedFigure.selectable = true;
	            selectedFigure = null;
				
			 trace("Figure  selected ,Mouse in null position");
			}
			else
			 trace("Figure not selected");
			 
		}
		
		private function mouseOverHandler(evnt:MouseEvent):void 
		
		{

			trace(evnt.target.name );
			var str:String = evnt.target.name;
			var i:int = str.indexOf("_");
			var str2:String=str.slice(0, i);
			/*if (evnt.target is actor) {
				
				trace("hint!!!!");
				
				Hint.show("it is actor " + evnt.target );
			}*/
			 
			switch(str2)
			{
				case "actorDomino":
					Hint.show("You can push this rectangle");
				break;
				case "actorCircle":
					Hint.show("You can push this circle");
				break;
				case "actorCoin":
					Hint.show("You should collect all these stars");
				break;
				
				
				
				
			}
			
		}//end
		
		
		
		
		
		private function downListener(e:MouseEvent):void 
		{
			trace("down");
			if(selectedFigure is actorCircle)
			 actorCircle(selectedFigure).circle.startDrag();
			if(selectedFigure is actorDomino)
			 actorDomino(selectedFigure).dominSprite.startDrag();
			if(selectedFigure is actorTrident)
			 actorTrident(selectedFigure).dominSprite.startDrag();
			if(selectedFigure)
			 selectedFigure.select = true;
			 
			 mouseDown = true;
		}
		
		private function outListener(e:MouseEvent):void 
		{
			if(selectedFigure is actorCircle)
			 actorCircle(selectedFigure).circle.startDrag();
			if(selectedFigure is actorDomino)
			 actorDomino(selectedFigure).dominSprite.startDrag();
			if(selectedFigure is actorTrident)
			 actorTrident(selectedFigure).dominSprite.startDrag();
			selectedFigure.select = false;
			trace("OUT");
			mouseDown = false;
		}
		
		private function overHandler(e:eventRun):void 
		{
			trace("TODO:RESTART");
			run = false;
			//menu.restartHandler(null);
		}
		
		private function deleteCoin(e:eventLoadLevel):void 
		{
			for each(var del:actorCoin in e.data){
			 dominToDelete.push(e.data[0]);
			 
			 var movingStar:Sprite;
			 if(del is actorCoin){
			  if(del.friction!=0)
			   movingStar = new starSVG();
			  else
			   movingStar = new starSVG2();
			 
			 movingStar.x = e.data[0]._costume.x;
			 movingStar.y = e.data[0]._costume.y;
			 movingStar.scaleX = e.data[0]._costume.scaleX;
			 movingStar.scaleY = e.data[0]._costume.scaleY;
			 
			 //movingStar.stop();
			 addChild(movingStar);
			 
			/*var tweenX:KSTween = new KSTween(movingStar, "x", movingStar.x, 620, "3sec", "0sec", Cubic.easeInOut);
			var tweenY:KSTween = new KSTween(movingStar, "y", movingStar.y, 330, "3sec", "0sec", Cubic.easeInOut);*/
			var tweenX:KSTween = new KSTween(movingStar, "x", movingStar.x,movingStar.x+Math.random()*100-50 , "3sec", "0sec",  Cubic.easeOut);
			var tweenY:KSTween = new KSTween(movingStar, "y", movingStar.y, 590, "6sec", "0sec", Cubic.easeOut);
			
			var tweenRot:KSTween = new KSTween(movingStar, "rotation", movingStar.rotation,(Math.random()-0,5)*4*360, "20sec", "0sec", null);
			


			
			tweenX.start();
			tweenY.start();
			tweenRot.start();
    		coinArray.push(movingStar);
			 }
			
			}	//trace(e.data[0]);
		}//end
		

private function keyPressedDown(event:KeyboardEvent):void {
      var key:uint = event.keyCode;
 //var key2:uint= event.keyCode
      var step:uint = 5;
	 
	//  trace("selectedFigure"+selectedFigure);
	 // trace("key!!!");
      switch (key) {
       
		  case Keyboard.LEFT :
         
		  if (selectedFigure) {
			//selectedFigure._body.
			//getAngleFromArray();
			  actorDomino(selectedFigure).angleIndex--;
			  selectedFigure._body.SetXForm(new b2Vec2(stage.mouseX/30, stage.mouseY / 30),Math.round((actorDomino(selectedFigure).angleIndex)*15)*Math.PI/180  );
		     getDataFromDomin();
		   
			  trace("selectedFigureAngle= " + selectedFigure._body.GetAngle()*180/Math.PI);
		   }
		   break;
		   
		   
          case Keyboard.RIGHT :
	      
		  if (selectedFigure) {
			  actorDomino(selectedFigure).angleIndex++;
		        selectedFigure._body.SetXForm(new b2Vec2(stage.mouseX/30, stage.mouseY / 30),Math.round((actorDomino(selectedFigure).angleIndex)*15)*Math.PI/180  );
		    getDataFromDomin();
		   trace("selectedFigureAngle= " + selectedFigure._body.GetAngle()*180/Math.PI);
		   }
		   break;
         
		   case 68 :
         
		  if (selectedFigure) {
			//selectedFigure._body.
			  actorDomino(selectedFigure).angleIndex++;
			  selectedFigure._body.SetXForm(new b2Vec2(stage.mouseX/30, stage.mouseY / 30),Math.round((actorDomino(selectedFigure).angleIndex)*15)*Math.PI/180  );
		       getDataFromDomin();
		   
			   trace("selectedFigureAngle= " + selectedFigure._body.GetAngle()*180/Math.PI);
		   }
		   break;
		  
        case 65 :
	      
		  if(selectedFigure){
		  actorDomino(selectedFigure).angleIndex--;
		        selectedFigure._body.SetXForm(new b2Vec2(stage.mouseX/30, stage.mouseY / 30),Math.round((actorDomino(selectedFigure).angleIndex)*15)*Math.PI/180  );
		    getDataFromDomin();
		    trace("selectedFigureAngle= " + selectedFigure._body.GetAngle()*180/Math.PI);
		   }
		   break;
        case 82:
		  menu.restartHandler(null); 
		//parent.dispatchEvent(new eventLoadLevel(eventLoadLevel.CLEAR_LEVEL));
		break;
		   
		default:
			break;
      }
    }//end
	
	private function intAngleArray():void
	{
		for (var i:int = 0; i < 32; i++ ) {
			angleArray.push(Math.PI / 16); }
	}//end
	
	
		private function timeFps(e:TimerEvent):void 
		{
			menu.fps.text = String(fps);
			fps = 0;
		}//end
		
		 public function circleDraw(x:Number,y:Number):void {
	     var circleDrawing5:Sprite= new Sprite(); 
		 circleDrawing5.graphics.clear();
	     circleDrawing5.graphics.lineStyle(1);
		 circleDrawing5.graphics.beginFill(0xFFCC00, 1);
		 
		
          circleDrawing5.graphics.drawCircle(x*30,y*30, 3);
		  
         circleDrawing5.visible = true;
		 addChild(circleDrawing5);
	  }//end
	  
		private function fpsHandler(e:TimerEvent):void 
		{
			trace("time complete");
		}//end
		
		
		
		private function initArrayForScores(n:int):void{
		trace(myDataObj.size + " =myDataObj.size");
			if(myDataObj.size==0){
			 for (var i:int = 0; i < n; i++ ) {
				
			     levels_scores.push(0);
			 
			    if((i%6==5)||(i>=24))
			     levelsAccess.push(false);
			    else
			     levelsAccess.push(true);
			    
				 levelsAccess[0] = true;
				}
			}
				else
				{
					trace(myDataObj.size + " =myDataObj.size222");

					levels_scores = myDataObj.data.arrayScore;
			        common_scores = myDataObj.data.score;
					levelsAccess = myDataObj.data.arrayAccess;
					
					//menu.reloadScoresArray(levels_scores);
			  for (var j:int = 0; j < n; j++ ) {
			      if ((levels_scores[j] != 0)||(levelsAccess[j] == true))
				    levelsAccess[j] = true;
				  else
				    levelsAccess[j] = false;
				   
				  }
				  
				  levelsAccess[0] == true;
					}
			
		}//end
		
	
		
		private function stopHandler(e:eventRun):void { //level completed
			run = false;
			if (menu.clickNumber != 0){
			  menu.current_scores += 500 * menu.clickNumber;
			 }
			//levels_scores[menu.main_level]
			 LevelEnd = true;
			 
			 if(levels_scores[menu.main_level]<=menu.current_scores){
			  levels_scores[menu.main_level] = menu.current_scores;
			  menu.reloadScoresArray(levels_scores);
			 }
			  
			 
			common_scores = countScores(); 
			menu.scores.text = "SCORES "  + String(menu.current_scores);
  			  updateShareData();
			  
			menu.addScores(common_scores);
			trace(common_scores);
			menu.timer.reset();
			trace("stop");
			//if(menu.main_level)
			for (var i:int = 0; i < 5; i++ ){
			 var allOpen:int=0;
			 for (var j:int = 0; j < 5; j++ ) {
				  if (levels_scores[j + i * 6] == 0)
				   break;
				  else {
					  allOpen ++;
				  }
			 }
			 if((allOpen>=5)&&((i * 6 + 5)!=29)&&(levelsAccess[i * 6 + 5] == false)){	  
			  levelsAccess[i * 6 + 5] = true;
				  trace("unlock level = " + String(i * 6 + 5));
				  menu.unlockLevel(i * 6 + 5);
			 }
				  
			}
			
			for (i = 0; i < 6; i++ ){
			 var allOpen2:int=0;
			 for (j = 0; j < 24; j+=6 ) {
				  if (levels_scores[j+i] == 0)
				   break;
				  else {
					  allOpen2 ++;
				  }
			 }
			 if((allOpen2>=4)&&((j+i)!=29)&&(levelsAccess[i +j] == false)){	  	  
			  levelsAccess[j+i] = true;
				  trace("unlock level = " + String(i+j));
				  menu.unlockLevel(i+j);
			 }
				  
			}
			 var last:int=0;
				
					if ((levelsAccess[28]==true)&&(levelsAccess[23]==true)&&(levelsAccess[29]==false)){
					  menu.unlockLevel(29);
					  levelsAccess[29] = true;
					}
			menu.setLevelAccess(menu.main_level);		 
					 
		//	menu.setLevelAccess(menu.main_level - 1);
			/*if(  ((menu.main_level - 1)<=29) &&( (menu.main_level - 1)>=0)&&(menu.main_level%5!=0) ){
			  menu.setLevelAccess(menu.main_level - 1);
			  levelsAccess[menu.main_level-1] = true;
			 }
			 if(  ((menu.main_level  -5)<=29)&&( (menu.main_level - 5)>=0) ){
			  menu.setLevelAccess(menu.main_level - 5);
			  levelsAccess[menu.main_level-5] = true;
			 }
			if(  ((menu.main_level + 5)<=29)&&( (menu.main_level + 5)>=0) ){
			  menu.setLevelAccess(menu.main_level + 5);
			  levelsAccess[menu.main_level+5] = true;
			 }
			
			 if(  ((menu.main_level + 1)<=29)&&((menu.main_level + 1)>=0) ){
			  menu.setLevelAccess(menu.main_level + 1);
			  levelsAccess[menu.main_level+1] = true;
			 }*/
			
			//levelsAccess[menu.main_level+1] = true;
			
			menu.hideLevels();
		}//end
		
		private function countScores():Number{
			var count:Number = 0;
			
			for each(var sc:Number in levels_scores)
			 count += sc;
			return count;
		} //end
		
		private function upListener(e:MouseEvent):void 
		{
			trace("out");
			if (selectedFigure is actor) {
				if (!checkMousePosition()) {
					trace(pointXY.x, pointXY.y);
					//trace();
					
					trace("not right zone");
					}
			//checkMousePosition();
			 if ((numberSelectable == 0)&&(!LevelEnd))
	          run = true;
			}
		}//end
		
	    private function reloadScores(e:menuEvents):void 
		{
			menu.reloadScores(levels_scores);
		}//end
        
		private function createDomin():void {
	      trace("create some");
	           if (typeFigure != "") {   
				   
					if (typeFigure == "[object actorDomino]") {
		             domin = new actorDomino(m_world, this, new Point(stage.mouseX, stage.mouseY),Number(data[6]),1,Number(data[4]),Number(data[2]),Number(data[3]),Number(data[7]),isSensor);
					 domin.addEventListener(OutOfScreen.OUT_DOMIN, removeHandler);
					
					 }
	
					 if (typeFigure == "[object actorTriangle]") {
		             domin = new actorTriangle(m_world, this, new Point(stage.mouseX, stage.mouseY),Number(data[6]),1,Number(data[4]),Number(data[2]),Number(data[3]),Number(data[7]),isSensor);
					 domin.addEventListener(OutOfScreen.OUT_DOMIN, removeHandler);
					 }
					 
				    if(typeFigure=="[object actorCircle]"){
		             domin = new actorCircle(m_world, this,new Point(stage.mouseX, stage.mouseY),Number(data[6]),1,Number(data[4]),Number(data[2]),Number(data[3]),Number(data[7]),isSensor);
					 domin.addEventListener(OutOfScreen.OUT_DOMIN,removeHandler);
					 }
					 
					if (typeFigure == "[object actorCross]")
					 domin = new actorCross(m_world, this,new Point(stage.mouseX, stage.mouseY),Number(data[6]),Number(data[5]),Number(data[4]),Number(data[2]),Number(data[3]),Number(data[7]),Number(data[8]),isSensor);
		            
					 if (typeFigure == "[object actorAngle]")
                      domin = new actorAngle(m_world, this,new Point(stage.mouseX, stage.mouseY),Number(data[6]),Number(data[5]),Number(data[4]),Number(data[2]),Number(data[3]),Number(data[7]),Number(data[8]),isSensor);  
					 
					 if (typeFigure == "[object actorTrident]")
                      domin = new actorTrident(m_world, this,new Point(stage.mouseX, stage.mouseY),Number(data[6]),Number(data[5]),Number(data[4]),Number(data[2]),Number(data[3]),Number(data[7]),Number(data[8]),isSensor);  
					 
					 if (typeFigure == "[object actorTrident2]")
                      domin = new actorTrident2(m_world, this,new Point(stage.mouseX, stage.mouseY),Number(data[6]),Number(data[5]),Number(data[4]),Number(data[2]),Number(data[3]),Number(data[7]),Number(data[8]),isSensor);  
					  
					 if (typeFigure == "[object actorPendulum]")
                      domin = new actorPendulum(m_world, this,new Point(stage.mouseX, stage.mouseY),Number(data[6]),Number(data[5]),Number(data[4]),Number(data[2]),Number(data[3]),Number(data[7]),Number(data[8]),isSensor);  
					 
					 if (typeFigure == "[object actorPendulum2]")
                      domin = new actorPendulum2(m_world, this,new Point(stage.mouseX, stage.mouseY),Number(data[6]),Number(data[5]),Number(data[4]),Number(data[2]),Number(data[3]),Number(data[7]),Number(data[8]),isSensor);  
					 
					  if(typeFigure=="[object actorCoin]"){
		             domin = new actorCoin(m_world, this,new Point(stage.mouseX, stage.mouseY),Number(data[6]),Number(data[5]),Number(data[4]),Number(data[2]),Number(data[3]),Number(data[7]));
	               
					}
                     dominToDelete.push(selectedFigure);					
					 selectedFigure = domin;
				     dominArray.push(domin);
					 }
	}//end
		
		private function deleteDomino(e:eventLoadLevel):void 
		{
			if(dominToDelete.length<1)
			 dominToDelete.push(e.data[0]);
			
		}
		private function clearHandler(e:eventLoadLevel):void 
		 {
			//dominToDelete.push(e.data);
			for each(var h:MovieClip in menu.levels2.hintArray)
			 removeChild(h);
			  
			 hintAdded = false;
			 menu.levels2.hintAdded = false;
			 menu.levels2.deleteHints();
			 removeDominos();
			// run = false;
		 }//end
		 
		 
		private function loadDominos(e:eventLoadLevel):void 
		{
			dominArray = e.data;
			if(bg_bm)
			 bg_bm.remove(this);
			bg_bm = new BG2(this);
			
			numberSelectable = 0;
			menu.clickNumber = menu.levels2.clicks;
			menu.clicks.text ="CLICKS " + String(menu.clickNumber);
		//	trace("menu.clickNumber= " + menu.clickNumber);
			LevelEnd = false;
			for each (var dAct:actor in dominArray){ // check selectable
			 if (dAct.markAsSelectable())
			    numberSelectable++;
			//	trace(numberSelectable+ "numberSelectable");
				run = false;
			}
			
		//	trace(numberSelectable+ " = numberSelectable");
			menu.coin.text = "STARS " + String(int(menu.coins));
			menu.target_coin.text = "TARGET " + String(menu.levels2.getCoins());
			
		}//end
		
		private function runGame(e:Event):void 
		{
			trace("run game");
			run = true;
		}//end
	
	public function drawLine(x:Number,y:Number,_rotation:Number):b2Vec2
	{
        
		 var intersect:b2Vec2 = new b2Vec2(0, 0);
		 var intersect2:b2Vec2 = new b2Vec2(0, 0);
         var intersect3:b2Vec2 = new b2Vec2(0, 0);
		 var intersect4:b2Vec2 = new b2Vec2(0, 0);

		var a:Point= new Point(0,0);
		var b:Point= new Point(0,0);
		var a2:Point= new Point(0,0);
		var b2:Point= new Point(0,0);
		
		
		if (aboveMouse.GetUserData() is actorCircle) {
			rotation3 = aboveMouse.GetUserData().GetArrowAngle(new Point(stage.mouseX, stage.mouseY), new Point(aboveMouse.GetPosition().x*30,aboveMouse.GetPosition().y*30),aboveMouse.GetUserData()._costume.rotation) ;
		    //rotation += 90;
		   //trace(_rotation + "circle rotation");
	    	a.x = aboveMouse.GetPosition().x*30 + actorCircle(aboveMouse.GetUserData()).rad * Math.sin(rotation3);
		    a.y = aboveMouse.GetPosition().y * 30 - actorCircle(aboveMouse.GetUserData()).rad * Math.cos(rotation3);
			var d:Number=Point.distance(new Point(aboveMouse.GetPosition().x * 30, aboveMouse.GetPosition().y * 30), new Point(a.x, a.y));
		// trace(d + " = distance");
		 //trace((aboveMouse.GetUserData()).circle.width/2 + " = width");
			/*	 circleDrawing.graphics.clear();
		 circleDrawing.graphics.lineStyle(1);
		 circleDrawing.graphics.beginFill(0xFFCC00, 1);
		 if(intersect)
          circleDrawing.graphics.drawCircle(a.x, a.y, 3);
		  
         circleDrawing.visible = true;
		 addChild(circleDrawing);
		 
		 
		  lineDrawing.graphics.clear();
         lineDrawing.graphics.lineStyle(1);
         lineDrawing.graphics.moveTo(aboveMouse.GetPosition().x*30, aboveMouse.GetPosition().y*30); 
		  lineDrawing.graphics.lineTo(stage.mouseX,stage.mouseY);
         lineDrawing.graphics.lineTo(a.x,a.y);
		 lineDrawing.visible = true;
		 addChild(lineDrawing);*/
			return new b2Vec2(a.x / 30, a.y / 30);
	
			}
		
		
		if ((aboveMouse.GetUserData() is actorTriangle)||(aboveMouse.GetUserData() is actorDomino)) {
		
		var rotation2:Number = _rotation + 90;
		_rotation *= Math.PI / 180;
		_rotation += Math.PI /2;
		
		rotation2 *= Math.PI / 180;
		rotation2 += Math.PI /2;
		
		//1
		a.x = x+20 * Math.cos(_rotation);
		a.y = y+20 * Math.sin(_rotation);
		b.x = x-20 * Math.cos(_rotation);
		b.y = y - 20 * Math.sin(_rotation);
		//2
		a2.x = x+20 * Math.cos(rotation2);
		a2.y = y+20 * Math.sin(rotation2);
		b2.x = x-20 * Math.cos(rotation2);
		b2.y = y - 20 * Math.sin(rotation2);
		 var arr:Array = actor(aboveMouse.GetUserData()).CheckIntersection(aboveMouse.GetUserData(), new b2Vec2(a.x/30,a.y/30), new b2Vec2(b.x/30,b.y/30),new b2Vec2(x/30,y/30));
         var arr2:Array = actor(aboveMouse.GetUserData()).CheckIntersection(aboveMouse.GetUserData(), new b2Vec2(a2.x/30,a2.y/30), new b2Vec2(b2.x/30,b2.y/30),new b2Vec2(x/30,y/30));
    //  trace(stage.mouseX+ "mouse coord" +stage.mouseY);
	    var lengthArray:Array = [];
		lengthArray.push(arr[1]);
		lengthArray.push(arr[3]);
		lengthArray.push(arr2[1]);
		lengthArray.push(arr2[3]);
        lengthArray.push(arr[0]);
		lengthArray.push(arr[2]);
		lengthArray.push(arr2[0]);
		lengthArray.push(arr2[2]);
        var pointImpulse:Number = 0;
	 k = -1;
		if(lengthArray[0]){
		 k = 0;
			pointImpulse = lengthArray[0];
		}
		else 
		 pointImpulse = 100;
		 
		// trace("0 " + lengthArray[0]);
		for (var i:int=1; i < 4;i++ ) {
		// trace(i +" "+ lengthArray[i]);	
		//trace(pointImpulse+ "pointImpulse");
			if ((pointImpulse > lengthArray[i])&&(lengthArray[i])) {
		//trace("up"); 
			   pointImpulse = lengthArray[i];
			k = i;
			}
		}
	    //trace("k"+k);
		intersect = lengthArray[k + 4];
		}
		//intersect2 = lengthArray[5 ];
	//	intersect3 = lengthArray[6 ];
		//intersect4 = lengthArray[7 ];
		
		
/*		 lineDrawing.graphics.clear();
         lineDrawing.graphics.lineStyle(1);
         lineDrawing.graphics.moveTo(a.x,a.y); 
         lineDrawing.graphics.lineTo(b.x, b.y);
		 lineDrawing.visible = true;
		 addChild(lineDrawing);
		 
		 lineDrawing2.graphics.clear();
         lineDrawing2.graphics.lineStyle(1);
         lineDrawing2.graphics.moveTo(a2.x,a2.y); 
         lineDrawing2.graphics.lineTo(b2.x, b2.y);
		 lineDrawing2.visible = true;
		 addChild(lineDrawing2);
		//1 
		 circleDrawing.graphics.clear();
		 circleDrawing.graphics.lineStyle(1);
		 circleDrawing.graphics.beginFill(0xFFCC00, 1);
		 if(intersect)
          circleDrawing.graphics.drawCircle(intersect.x * 30, intersect.y * 30, 3);
		  
         circleDrawing.visible = true;
		 addChild(circleDrawing);*/
		 
	/*	 circleDrawing2.graphics.clear();
		 circleDrawing2.graphics.lineStyle(1);
		 circleDrawing2.graphics.beginFill(0xFF0000, 1);
		 if(intersect2)
          circleDrawing2.graphics.drawCircle(intersect2.x*30, intersect2.y*30, 3);
        
		 circleDrawing2.visible = true;
		 addChild(circleDrawing2);
		//2 
		  circleDrawing3.graphics.clear();
		 circleDrawing3.graphics.lineStyle(1);
		 circleDrawing3.graphics.beginFill(0xFFCC00, 1);
		 if(intersect3)
          circleDrawing3.graphics.drawCircle(intersect3.x * 30, intersect3.y * 30, 3);
		  
         circleDrawing3.visible = true;
		 addChild(circleDrawing3);
		 
		 circleDrawing4.graphics.clear();
		 circleDrawing4.graphics.lineStyle(1);
		 circleDrawing4.graphics.beginFill(0xFF0000, 1);
		 if(intersect4)
          circleDrawing4.graphics.drawCircle(intersect4.x*30, intersect4.y*30, 3);
        
		 circleDrawing4.visible = true;
		 addChild(circleDrawing4);
		 */
		 
		
		 if(intersect)
		   return intersect;
		  else return null;
		  
		 
        // lineDrawing.graphics.lineTo(600, 400);
		}//end

		
	private function getDataFromDomin():void{
		 	 
		 
		    if(selectedFigure is actorDomino)
				   data = actorDomino(selectedFigure).getData();

			if(selectedFigure is actorTriangle)
				   data = actorTriangle(selectedFigure).getData();  
			
			if(selectedFigure is actorCircle)
				   data = actorCircle(selectedFigure).getData();

			if(selectedFigure is actorCross)
				   data = actorCross(selectedFigure).getData();
				   
			if(selectedFigure is actorCoin)
				   data = actorCoin(selectedFigure).getData();
				   
		    if(selectedFigure is actorAngle)
			       data = actorAngle(selectedFigure).getData();
				   
			if(selectedFigure is actorTrident)
			       data = actorTrident(selectedFigure).getData();
			
			if(selectedFigure is actorTrident2)
			       data = actorTrident2(selectedFigure).getData();
				   
			if(selectedFigure is actorPendulum)
			       data = actorPendulum(selectedFigure).getData();
			
			if(selectedFigure is actorPendulum2)
			       data = actorPendulum2(selectedFigure).getData();
		
	}//end
	
   private function clickListener(e2:MouseEvent):Boolean {   
	 trace("click")
	  actDomin = null;
	 
	    aboveMouse = GetBodyAtMouse();		
		
		checkMousePosition();//this function generate events and selects domins
			
	if ((run==true)&&(aboveMouse)){  //push domino
		dominToMove = actDomin; 
		 
//trace(actDomin.getActorPosition().x + " Actor position " + actDomin.getActorPosition().y);
		if (menu.clickNumber > 0)   {
		//	trace(actDomin.getActorPosition().x + " Actor position2 " + actDomin.getActorPosition().y);
		 
		if (actDomin is actorDomino){
		 actDomin.moveDomino(intersect, actDomin.getActorPosition(), actDomin._costume.rotation, arrowSprite.rotation, k );
		 menu.music.PlaySound("rectangle");
		}
		 if (actDomin is actorCircle){
		   actDomin.moveDomino(intersect, actDomin.getActorPosition(), actDomin._costume.rotation, arrowSprite.rotation, k );
		   menu.music.PlaySound("circle");
		   }
		 if (actDomin is actorTriangle)
		   actDomin.moveDomino(intersect,actDomin.getActorPosition(),actDomin._costume.rotation,arrowSprite.rotation,k );
		 
		//  sound1.play();
		  menu.clickNumber--;
		  menu.clicks.text = "CLICKS " + String(menu.clickNumber);
		  
		  }
		   
	    }	
		
			
	return true;
}//end
///////////////////////////////////
private function menuHandler(event:MainField):void {
	 aboveMouse = GetBodyAtMouse();
	 
if(aboveMouse)
	 actDomin = aboveMouse.GetUserData();
   
	else
	 actDomin = null;
	 
	if(actDomin){
	 trace("we select figure" + actDomin);
	 typeFigure = actDomin.toString();
	 }
	else
	 trace("select some figure");
	}//end

private function fieldHandler(event:MainField):void {
	
	aboveMouse = GetBodyAtMouse();
	
	if (selectedFigure is actor) {
	    if (count > 1){
		 dispatchEvent(new MainField(MainField.MOUSE_IN_NULL));
	     return ;  
		}
		numberSelectable--;
		trace("numberSelectable after -- = "+ numberSelectable)
	    selectedFigure = null;
		
	  }
	 
	   
	
	
	if(aboveMouse){
	 actDomin = aboveMouse.GetUserData();
	 typeFigure = actDomin.toString();
	}
	 
	if(actDomin){
		 trace ("we selected this body " + actDomin);	   
	 }
	
	}//end	
private function selectHandler(e:MainField):Boolean {
	aboveMouse = GetBodyAtMouse();
   if (selectedFigure is actor) {
	  
	   //trace(pointXY + " pointXY");
	   if (typeFigure == "[object actorDomino]") {
		             domin = new actorDomino(m_world, this, new Point(pointXY.x, pointXY.y),Number(data[6]),Number(data[5]),Number(data[4]),Number(data[2]),Number(data[3]),Number(data[7]),isSensor);
					 domin.addEventListener(OutOfScreen.OUT_DOMIN, removeHandler);
					 }
	
	   if (typeFigure == "[object actorTriangle]") {
		             domin = new actorTriangle(m_world, this, new Point(pointXY.x, pointXY.y),Number(data[6]),Number(data[5]),Number(data[4]),Number(data[2]),Number(data[3]),Number(data[7]),isSensor);
					 domin.addEventListener(OutOfScreen.OUT_DOMIN, removeHandler);
					 }
					 
	   if(typeFigure=="[object actorCircle]"){
		             domin = new actorCircle(m_world, this,new Point(pointXY.x, pointXY.y),Number(data[6]),Number(data[5]),Number(data[4]),Number(data[2]),Number(data[3]),Number(data[7]),isSensor);
					 domin.addEventListener(OutOfScreen.OUT_DOMIN,removeHandler);
					 }
					 
					 domin.selectable = true;
					 
					// domin.select = true;
				     
					 dominArray.push(domin);
					 dominToDelete.push(selectedFigure);					
					
					 // numberSelectable++;
	                 selectedFigure = null;
		return true;
	  }
  else {
		if( (aboveMouse) &&(aboveMouse.GetUserData().selectable) ){
	           //actDomin = 
			   selectedFigure = aboveMouse.GetUserData();
			   getDataFromDomin();
			   trace(selectedFigure._costume.x + " selectedFigure "+ selectedFigure._costume.y);
			   pointXY = new Point(selectedFigure._costume.x+selectedFigure._costume.width/2, selectedFigure._costume.y+selectedFigure._costume.height/2);
			   trace ("we selected this body " + selectedFigure);
	           typeFigure = selectedFigure.toString();
			   createDomin();
			    selectedFigure.select = true;
	         }
		else 
			 trace("select some figure");
       }
	  return false;		
   }//end
	
public function GetBodyAtMouse():b2Body {
			 var real_x_mouse:Number = (stage.mouseX)/pixels_in_a_meter;
			 var real_y_mouse:Number = (stage.mouseY)/pixels_in_a_meter;
		
			 var body_number:int=0;
			mousePVec.Set(real_x_mouse, real_y_mouse);
			var aabb:b2AABB = new b2AABB();
			aabb.lowerBound.Set(real_x_mouse - 0.001, real_y_mouse - 0.001);
			aabb.upperBound.Set(real_x_mouse + 0.001, real_y_mouse + 0.001);
			var k_maxCount:int = 10;
			var shapes:Array = new Array();
			count= m_world.Query(aabb, shapes, k_maxCount);
			//if(count)
			 //trace(count+ " count");
			var body:b2Body = null;
			var count2:int = count;
			for (var i:int = 0; i < count2; ++i) {
				if ((shapes[i].GetBody().GetUserData() is actorCoin) || (shapes[i].GetBody().GetUserData() is actorCross)) {
					count--;
					}
				    var tShape:b2Shape = shapes[i] as b2Shape;
					var inside:Boolean = tShape.TestPoint(tShape.m_body.GetXForm(), mousePVec);
					
					if (inside) {
						
						
					//trace(body+ "body");
						body = tShape.m_body;
						if (body.GetUserData() is domino.actorCross)
						 trace(body.GetUserData() + "body");
						//trace(body.GetUserData() + "body");
						break;
					}
				//if (count > 1)
				//trace(count + "count");
			}		 	
			return body;
		}	//end
		

	
		private function testShape(act:actor,sh:b2Shape):void
		{
			var shapes:Array = []; 
			
			shapes=actorDomino(act).getShapes();
		    var index:int = shapes.indexOf(sh);
		 //   trace(index);
			actorDomino(act).setCurrentShape(index);
			normals = b2PolygonShape(sh).GetNormals();
		//	trace(normals.length);
			//trace("test");
		}//end
		
		
   public function Update(e:Event):void {
		 
		 if(run)
		   m_world.Step(1 / 30, m_iterations);
		 // trace(stage.mouseX,stage.mouseY);
		   fps++;
		//   trace(selectedFigure +" sel figure" );
		// else trace("not run");  
		  aboveMouse = GetBodyAtMouse(); 
		  
		  if((aboveMouse)&&(run==true)){
		   if ((( aboveMouse.GetUserData() is actorTriangle)||( aboveMouse.GetUserData() is actorCircle) || (aboveMouse.GetUserData() is actorDomino))&&(aboveMouse.GetUserData().density!=0)) {
			   
			  intersect= drawLine(stage.mouseX,stage.mouseY,aboveMouse.GetUserData()._costume.rotation);
			
			 if(intersect){
			  arrowSprite.x =intersect.x*30 ;
		      arrowSprite.y = intersect.y * 30;
			  arrowSprite.visible = true;
			  
			   if((aboveMouse.GetUserData() is actorTriangle)||(aboveMouse.GetUserData() is actorDomino))
			     arrowSprite.rotation = 180/Math.PI*getArrowRotation(aboveMouse.GetUserData()._costume.rotation,k); //aboveMouse.GetUserData()._costume.rotation; //180 / Math.PI * aboveMouse.GetUserData().GetArrowAngle(new Point(stage.mouseX, stage.mouseY), new Point(aboveMouse.GetPosition().x*30,aboveMouse.GetPosition().y*30),aboveMouse.GetUserData()._costume.rotation) ;
			   else
			     arrowSprite.rotation = 180 / Math.PI * (rotation3+Math.PI);
			  }
			  
			  }
		  }
		  else 
		  {
			  arrowSprite.visible = false;	  
			
		  }
         if (selectedFigure is actor) {
		
          reloadDomin();
		 }
	  
		    markToRemoveDomins();
		    realDominsDeleting();
			dominToDelete = [];
			
			for each(var coin2:Sprite in coinArray)	{
			   
				if (coin2.y >= 590) {
					
					removeChild(coin2);
					markCoin(coin2);
					}
			}
			realCoinDelete();
			if((menu.levels2)&&(menu.levels2.hintAdded))
			 hintsUpdate();
			
			for each(var something:actor in dominArray){
			  something.update();
			 // circleDraw(something._body.GetPosition().x, something._body.GetPosition().y);
			}
			  
			
		}//end    
		
		private function hintsUpdate():void
		{
					var j:int = 0;
					var hint:MovieClip;
			if ((!hintAdded)&&(menu.main_level == 0 ) && (menu.levels2)) {
				trace("hints!!!!!!!!!!!!!!!!!!");
				
				 addHints(menu.levels2.hintArray);
				 hintAdded = true;
				
		
			 for each(hint in menu.levels2.hintArray) {
			  	 hintArray[j][0]= new KSTween(hint, "x", menu.levels2.hintCoordArray[j][0],menu.levels2.hintCoordArray[j][1] , menu.levels2.hintDelayArray[j][0], menu.levels2.hintDelayArray[j][1],  Cubic.easeOut);
			     hintArray[j][1] = new KSTween(hint, "y", menu.levels2.hintCoordArray[j][2], menu.levels2.hintCoordArray[j][3], menu.levels2.hintDelayArray[j][2], menu.levels2.hintDelayArray[j][3], Cubic.easeOut);
	             
				 j++;
				}
				 sequence = new KSSequenceGroup(new KSParallelGroup(hintArray[0][0], hintArray[0][1]),new KSParallelGroup(hintArray[1][0], hintArray[1][1]),new KSParallelGroup(hintArray[2][0],hintArray[2][1]), new KSParallelGroup(hintArray[3][0], hintArray[3][1]),new KSParallelGroup(hintArray[4][0], hintArray[4][1]),new KSParallelGroup(hintArray[5][0], hintArray[5][1]));
			  sequence.start();
			}
		if ((!hintAdded)&&(menu.main_level == 1 ) && (menu.levels2)) {
				trace("hints!!!!!!!!!!!!!!!!!!");
				
				 addHints(menu.levels2.hintArray);
				 hintAdded = true;
				
			
			 for each(hint in menu.levels2.hintArray) {
			  	 hintArray[j][0]= new KSTween(hint, "x", menu.levels2.hintCoordArray[j][0],menu.levels2.hintCoordArray[j][1] , menu.levels2.hintDelayArray[j][0], menu.levels2.hintDelayArray[j][1],  Cubic.easeOut);
			     hintArray[j][1] = new KSTween(hint, "y", menu.levels2.hintCoordArray[j][2], menu.levels2.hintCoordArray[j][3], menu.levels2.hintDelayArray[j][2], menu.levels2.hintDelayArray[j][3],Cubic.easeOut);
	             
				 j++;
				}
				 sequence = new KSSequenceGroup(new KSParallelGroup(hintArray[0][0], hintArray[0][1]),new KSParallelGroup(hintArray[1][0], hintArray[1][1]),new KSParallelGroup(hintArray[2][0],hintArray[2][1]),new KSParallelGroup(hintArray[3][0], hintArray[3][1]));
			  sequence.start();
			}	
		}//end
		
		private function addHints(a:Array):void{
			var hintX:KSTween;
			hintArray = [];
			for each(var hint2:MovieClip in menu.levels2.hintArray) {
				
			 addChildAt(hint2, 2);
			 hintArray.push(new Array(hintX,hintX));
			 hint2.visible = true;
		    }
		}//end
		
	private function realCoinDelete():void{
	    
		var n:int;
		
		if (coinToDelete.length > 0){
		  for each(var c:Sprite in coinToDelete)
		   {
			n = coinArray.indexOf(c);
			if(n!=-1)
		       coinArray.splice(n, 1);
		   }
		 }
		coinToDelete = [];
		}//end	 
		
		
		private function markCoin(m:Sprite):void{
			coinToDelete.push(m);
		}//end
		
		
		private function getArrowRotation(_r:Number,_k:int):Number{
			var rotationArrow:Number = _r;
			var index:int = _k;
			//trace(index + " index");
			//trace(rotationArrow + " rotationArrow");
			
		if (index == 3 ) {
			 return 3*Math.PI/2 + rotationArrow* Math.PI/180;
			 }
		 if (index == 1 ) {
			 return Math.PI+ rotationArrow* Math.PI/180 ;
			 }
		 if (index == 0 ) {
			 return  0 + rotationArrow* Math.PI/180;
			 }
		 if (index == 2 ) {
			 return Math.PI/2+rotationArrow* Math.PI/180;
			 }
			return rotationArrow;
		}

 private function checkMousePosition():Boolean {

//main field	 
	if ((stage.mouseX >= 0) && (stage.mouseY >=0) && (stage.mouseX < 552) && (stage.mouseY < 480)){
	 dispatchEvent(new MainField(MainField.MOUSE_IN_FIELD));
	 return true;
	}
//menu field	
	/*if ((stage.mouseX > 640) && (stage.mouseY >=0) && (stage.mouseX < 788) && (stage.mouseY < 480)) {
	 dispatchEvent(new MainField(MainField.MOUSE_IN_FIGURES));
	 return true;
	 }*/
//select figure
    if ((stage.mouseX >= 554) && (stage.mouseY >=18) && (stage.mouseX <=638) && (stage.mouseY < 268)) {
	 dispatchEvent(new MainField(MainField.MOUSE_IN_SELECT));
	 return true;
	 }
	 
	 if (1){
	     dispatchEvent(new MainField(MainField.MOUSE_IN_NULL));
		 return true;
	 }
	return false; 
	}//end
     private function removeDominos():void {
		    	for each(var d:actor in dominArray ){
				 //if ((d is actorDomino)||(d is actorCircle)||(d is actorCross)|| (d is actorCoin) || (d is actorAngle))
				 if (d is actor)
				  if (d is actorWall)
				   continue;
				  dominToDelete.push(d);
                }
			}//end
	 private function markToRemoveDomins():void {
		var nToDel:int;
		var n:int;
		if (dominToDelete.length > 0){
		  for each(var domToDel:actor in dominToDelete)
		   {
			n = dominArray.indexOf(domToDel);
			if(n!=-1)
		       dominArray.splice(n, 1);
		   }

		 }
		}
      //end
	  private function realDominsDeleting():void {
			if (dominToDelete.length > 0){
			 for each(var domToDel2:actor in dominToDelete )  
			  domToDel2.removeActor(m_world);
		
			}			 
		}	
     //end
	 
/*	private function recreateDomin() {
		
		   if (actDomin != null){
				if(actDomin is actor)   
			     dominToDelete.push(actDomin); 
		   }
		
		if(actDomin is actorDomino){
		             domin = new actorDomino(m_world, this, new Point(Number(edit_X.text), Number(edit_Y.text)),Number(edit_angle.text),Number(edit_density.text),Number(edit_friction.text),Number(edit_scaleX.text),Number(edit_scaleY.text),Number(edit_restitution.text),isSensor);
		             domin.addEventListener(OutOfScreen.OUT_DOMIN,removeHandler);
					 }
 		if(actDomin is actorCircle){
		             domin = new actorCircle(m_world, this,new Point(Number(edit_X.text), Number(edit_Y.text)),Number(edit_angle.text),Number(edit_density.text),Number(edit_friction.text),Number(edit_scaleX.text),Number(edit_scaleY.text),Number(edit_restitution.text),isSensor);
		             domin.addEventListener(OutOfScreen.OUT_DOMIN,removeHandler);
					 }
		if(actDomin is actorCross)
		             domin = new actorCross(m_world, this,new Point(Number(edit_X.text), Number(edit_Y.text)),Number(edit_angle.text),Number(edit_density.text),Number(edit_friction.text),Number(edit_scaleX.text),Number(edit_scaleY.text),Number(edit_restitution.text),Number(edit_angV.text),isSensor);

		if(actDomin is actorAngle)
		             domin = new actorAngle(m_world, this,new Point(Number(edit_X.text), Number(edit_Y.text)),Number(edit_angle.text),Number(edit_density.text),Number(edit_friction.text),Number(edit_scaleX.text),Number(edit_scaleY.text),Number(edit_restitution.text),Number(edit_angV.text),isSensor);
				 
		if(actDomin is actorCoin)
		             domin = new actorCoin(m_world, this,new Point(Number(edit_X.text), Number(edit_Y.text)),Number(edit_angle.text),Number(edit_density.text),Number(edit_friction.text),Number(edit_scaleX.text),Number(edit_scaleY.text),Number(edit_restitution.text));
		
		if(actDomin is actorTrident)
		             domin = new actorTrident(m_world, this,new Point(Number(edit_X.text), Number(edit_Y.text)),Number(edit_angle.text),Number(edit_density.text),Number(edit_friction.text),Number(edit_scaleX.text),Number(edit_scaleY.text),Number(edit_restitution.text),Number(edit_angV.text),isSensor);
		
		if(actDomin is actorTrident2)
		             domin = new actorTrident2(m_world, this,new Point(Number(edit_X.text), Number(edit_Y.text)),Number(edit_angle.text),Number(edit_density.text),Number(edit_friction.text),Number(edit_scaleX.text),Number(edit_scaleY.text),Number(edit_restitution.text),Number(edit_angV.text),isSensor);
		
		if(actDomin is actorPendulum)
		             domin = new actorPendulum(m_world, this,new Point(Number(edit_X.text), Number(edit_Y.text)),Number(edit_angle.text),Number(edit_density.text),Number(edit_friction.text),Number(edit_scaleX.text),Number(edit_scaleY.text),Number(edit_restitution.text),Number(edit_angV.text),isSensor);
		
		if(actDomin is actorPendulum2)
		             domin = new actorPendulum2(m_world, this,new Point(Number(edit_X.text), Number(edit_Y.text)),Number(edit_angle.text),Number(edit_density.text),Number(edit_friction.text),Number(edit_scaleX.text),Number(edit_scaleY.text),Number(edit_restitution.text),Number(edit_angV.text),isSensor);
					 
		 dominArray.push(domin);
		 actDomin = domin;
		 MovieClip(actDomin._costume).gotoAndStop(3);
		 
		trace("deleted old and created new domin");
		}//end
		*/
		private function removeHandler(e:OutOfScreen):void 
		{
		  dominToDelete.push(e.currentTarget);	
		}//end
///////////////////////////////////
 private function reloadDomin():void {
		
	//	trace(" reload domin");
		// trace("reload");
		selectedFigure._body.SetXForm(new b2Vec2(stage.mouseX/30, stage.mouseY / 30), int(data[6]) * Math.PI / 180 );
		//actDomin..SetXForm(new b2Vec2(int(edit_X.text)/30, int(edit_Y.text)/30),int(edit_angle.text)*Math.PI/180 );
		 
		
	/*	if(actDomin is actorCross)
		             actorCross(actDomin).rev_joint.SetXForm(new b2Vec2(int(edit_X.text) / 30, int(edit_Y.text) / 30), int(edit_angle.text) * Math.PI / 180 );
		
		if(actDomin is actorAngle)
		             actorAngle(actDomin).rev_joint.SetXForm(new b2Vec2(int(edit_X.text) / 30, int(edit_Y.text) / 30), int(edit_angle.text) * Math.PI / 180 );
		
		if(actDomin is actorTrident)
		             actorTrident(actDomin).rev_joint.SetXForm(new b2Vec2(int(edit_X.text) / 30, int(edit_Y.text) / 30), int(edit_angle.) * Math.PI / 180 );
		
		if(actDomin is actorTrident2)
		             actorTrident2(actDomin).rev_joint.SetXForm(new b2Vec2(int(edit_X.text) / 30, int(edit_Y.text) / 30), int(edit_angle.text) * Math.PI / 180 );
		
		if(actDomin is actorPendulum)
		             actorPendulum(actDomin).rev_joint.SetXForm(new b2Vec2(int(edit_X.text) / 30, int(edit_Y.text) / 30), int(edit_angle.text) * Math.PI / 180 );
		
		if(actDomin is actorPendulum2)
		             actorPendulum2(actDomin).rev_joint.SetXForm(new b2Vec2(int(edit_X.text) / 30, int(edit_Y.text) / 30), int(edit_angle.text) * Math.PI / 180 );
		*/
		 
		}//end
		private function addIcons():void {
			
	          var iconDomino:actorDomino = new actorDomino(m_world, this, new Point(450, 40), 0,0, 0.3, 0.5, 0.5,0,0);	
	          iconArray.push(iconDomino);
		
			  var iconTriangle:actorTriangle = new actorTriangle(m_world, this, new Point(450, 40), 0,0, 0.3, 0.5, 0.5,0,0);	
	          iconArray.push(iconTriangle);
		
	
	          var iconCircle:actorCircle= new actorCircle(m_world, this, new Point(465,40),0,0,0.3,0.5,0.5,0,0);		
	          iconArray.push(iconCircle);
	
	          var iconCross:actorCross = new actorCross(m_world, this, new Point(500, 36), 0, 0, 0.3, 0.5, 0.5,0,0,0);
	          iconArray.push(iconCross);
	
	          var iconCoin:actorCoin= new actorCoin(m_world, this, new Point(455,68),0,0,0.3,0.5,0.5,0);		
	          iconArray.push(iconCoin);
	
	          var iconAngle:actorAngle = new actorAngle(m_world, this, new Point(465, 68),0,0, 0.3, 0.5, 0.5,0,0,0);
	          iconArray.push(iconAngle);
	
	          var iconTrident:actorTrident = new actorTrident(m_world, this, new Point(510, 88),0, 0, 0.3, 0.5, 0.5,0,0,0);
	          iconArray.push(iconTrident);
	
	          var iconTrident2:actorTrident2 = new actorTrident2(m_world, this, new Point(480, 100),0, 0, 0.3, 0.5, 0.5,0,0,0);
	          iconArray.push(iconTrident2);
	
	          var iconPendulum:actorPendulum = new actorPendulum(m_world, this, new Point(515, 40), 0, 0, 0.3, 0.5, 0.5,0,0,0);
	          iconArray.push(iconPendulum);
	
	          var iconPendulum2:actorPendulum2 = new actorPendulum2(m_world, this, new Point(510, 100), 0, 0, 0.3, 0.5, 0.5,0,0,0);
	          iconArray.push(iconPendulum2);
	
}//end
          private function createWorld():void {
		    var environment:b2AABB = new b2AABB();	
		    var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
		    environment.lowerBound.Set(-100.0, -100.0);
			environment.upperBound.Set(100.0, 100.0);
			m_world = new b2World(environment, gravity, true);	
		    m_world.SetContactListener(new DominContactListener());
		}	//end
		private function  addArrow():void {
			 arrowSprite = new arrow();
			 arrowSprite.x = -10;
			 arrowSprite.y = -10;
			 stage.addChild(arrowSprite);
			}//end
	}
	
}